package com.koron.css2.electronicCred.service.credfile;

import com.google.common.collect.Lists;
import com.koron.css2.ApplicationConfig;
import com.koron.css2.baseConfig.bean.BaseAccessoryMetadataBean;
import com.koron.css2.baseConfig.bean.BaseReceiptAccessoryBean;
import com.koron.css2.baseConfig.mapper.BaseAccessoryMetadataMapper;
import com.koron.css2.baseConfig.mapper.BaseReceiptAccessoryMapper;
import com.koron.css2.electronicCred.Enum.ProjectTypeEunm;
import com.koron.css2.electronicCred.bean.*;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.GovServicesReqUtils;
import com.koron.util.Tools;
import org.apache.commons.io.FilenameUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

/**
 * 居民用水证件文件
 *
 * @author 杨嘉慧
 * @date 2023/3/9
 */
@Service("CusWaterCredFileService")
public class CusWaterCredFileService extends AbstractCredFileService {

    @Override
    public MessageBean<?> getCredFile(SessionFactory factory, UserInfoBean userInfo, ElectronicCredRequestBean param) throws Exception {
        GovServicesRequestBean gsParam = new GovServicesRequestBean();
        gsParam.setGovServiceId(ProjectTypeEunm.getGovServiceId(param.getProjectType()));
        gsParam.setBusiType(ProjectTypeEunm.getBusiType(param.getProjectType()));
        gsParam.setSystemId(ApplicationConfig.govServicesSystemId);
        CusWaterCredFileReqParamBean reqParam = JsonUtils.objectToPojoIgnoreNone(param.getParam(), CusWaterCredFileReqParamBean.class);
        gsParam.setParam(JsonUtils.objectToPojoIgnoreNone(reqParam, Map.class));
        MessageBean res = JsonUtils.jsonToPojo(GovServicesReqUtils
                .doPostToGovServices("/getCredFile", gsParam), MessageBean.class);
        if (res.getCode() != Constant.MESSAGE_INT_SUCCESS) {
            return res;
        }
        List<CredFileBean> credFiles = JsonUtils.jsonToListIgnoreNone(JsonUtils.objectToJson(res.getData()), CredFileBean.class);
        BaseAccessoryMetadataMapper metadataMapper = factory.getMapper(BaseAccessoryMetadataMapper.class);
        BaseReceiptAccessoryMapper mapper = factory.getMapper(BaseReceiptAccessoryMapper.class);
        List<CusWaterGetCredFileResponseBean> credFilesRes = Lists.newArrayList();
        //附件处理：存入附件表，给前端返回可匹配展示的附件数据
        for (CredFileBean credFile : credFiles) {
            BaseAccessoryMetadataBean metadata = new BaseAccessoryMetadataBean();
            String metadataId = Tools.getObjectId();
            metadata.setAccessoryPath(credFile.getMdFileRelativeUrl());
            metadata.setAccessoryName(credFile.getLicenseName()
                    + credFile.getMdFileRelativeUrl().substring(credFile.getMdFileRelativeUrl().lastIndexOf(".")));
            metadata.setId(metadataId);
            metadata.setAccessorySize(credFile.getFileSize());
            metadata.setAccessoryType(credFile.getFileType());
            metadata.setCreateAccount(userInfo.getUserInfo().getAcount());
            metadata.setCreateName(userInfo.getUserInfo().getName());
            metadata.setSourceFlag("1");
            //存入附件表
            metadataMapper.insert(metadata);
            BaseReceiptAccessoryBean bean = new BaseReceiptAccessoryBean();
            bean.setReceiptId(reqParam.getReceiptId());
            bean.setReceiptType(reqParam.getReceiptType());
            bean.setAccessoryNo(credFile.getApplicationCredCode());
            String baseReceiptAccessoryBeanId = Tools.getObjectId();
            bean.setId(baseReceiptAccessoryBeanId);
            bean.setMetadataId(metadataId);
            bean.setCreateAccount(userInfo.getUserInfo().getAcount());
            bean.setCreateName(userInfo.getUserInfo().getName());
            bean.setAccessoryName(FilenameUtils.getName(credFile.getMdFileRelativeUrl()));
            //存入单据附件表
            mapper.insert(bean);
            credFilesRes.add(new CusWaterGetCredFileResponseBean().accessoryNo(credFile.getApplicationCredCode())
                    .accessoryUrl(ApplicationConfig.getAccessoryUploadUrl() + "fileDownload?path=" + credFile.getMdFileRelativeUrl())
                    .accessoryName(metadata.getAccessoryName()).id(baseReceiptAccessoryBeanId));
        }
        res.setData(credFilesRes);
        return res;
    }
}
